﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_between_arrays
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly int[] domainNumbers;
        private readonly DomainPerson domainPerson;

        public class DomainPerson
        {
            public int[] Numbers { get; set; }
        }
        public class GuiPerson
        {
            public int[] Numbers { get; set; }
            public String[] NumbersAsString { get; set; }
        }

        public When_mapping_between_arrays()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            domainNumbers = new [] { 1, 2, 3, 4 };
            domainPerson = new DomainPerson { Numbers = domainNumbers };
        }

        [Fact]
        public void Should_work_between_same_type()
        {
            mapping.Relate(domain => domain.Numbers, gui => gui.Numbers);
            var guiPerson = mapping.Map(domainPerson, new GuiPerson());
            var guiNumbers = guiPerson.Numbers;

            Assert.Equal(domainNumbers.Length,guiNumbers.Length);
            Assert.Equal(domainNumbers[0], guiNumbers[0]);
            Assert.Equal(domainNumbers[1], guiNumbers[1]);
            Assert.Equal(domainNumbers[2], guiNumbers[2]);
            Assert.Equal(domainNumbers[3], guiNumbers[3]);
        }

        [Fact]
        public void Should_work_between_different_types()
        {
            mapping.AddConverter(Converting.BetweenIntAndString());
            mapping.Relate(domain => domain.Numbers, gui => gui.NumbersAsString);
            var guiPerson = mapping.Map(domainPerson, new GuiPerson());
            var guiNumbers = guiPerson.NumbersAsString;

            Assert.Equal(domainNumbers.Length, guiNumbers.Length);
            Assert.Equal(domainNumbers[0].ToString(), guiNumbers[0]);
            Assert.Equal(domainNumbers[1].ToString(), guiNumbers[1]);
            Assert.Equal(domainNumbers[2].ToString(), guiNumbers[2]);
            Assert.Equal(domainNumbers[3].ToString(), guiNumbers[3]);
        }

        [Fact]
        public void Should_not_be_same_object()
        {
            mapping.Relate(domain => domain.Numbers, gui => gui.Numbers);
            var guiPerson = mapping.Map(domainPerson, new GuiPerson());
            var guiNumbers = guiPerson.Numbers;

            domainNumbers[1] = 13;

            Assert.NotEqual(domainNumbers[1],guiNumbers[1]);
        }
    }
}
